﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CronusModel;
using Cronus.ViewModel.Data;

namespace Cronus.ViewModel
{
    /// <summary>
    /// Provides an interface to query the activities table in the data model.
    /// </summary>
    internal class QueryActivityInformation
    {
        //*** Constants ***//
        //*** Static ***//
        //*** Member Variables ***//
        private DataModel _DataModel;

        //*** Properties ***//
        //*** Constructors ***//
        internal QueryActivityInformation(DataModel aDataModel)
        {
            _DataModel = aDataModel;
        }

        //*** Abstract Methods ***//
        //*** Methods ***//
        /// <summary>
        /// Returns an enumerable collection of activities that started between the given start
        /// and end times (inclusive).
        /// </summary>
        /// <param name="aStartDate">Start period of the query.</param>
        /// <param name="anEndDate">End period of the query.</param>
        /// <returns>Enumerable collection of activities.</returns>
        internal IEnumerable<CronusData.ActivitiesRow> GetActivitiesInDateRange(DateTime aStartDate, DateTime anEndDate)
        {
            // Local variables
            IEnumerable<CronusData.ActivitiesRow> aQueryResult = null;

            // First, query for the correct date/time range.
            aQueryResult = from anActivity in _DataModel.ActivitiesTable
                           where ((aStartDate <= anActivity.StartTime) &&
                                  (anActivity.StartTime <= anEndDate))
                           orderby anActivity.StartTime ascending
                           select anActivity;
            return aQueryResult;
        }

        /// <summary>
        /// Queries for any activity that is currently being timed.
        /// </summary>
        /// <returns></returns>
        internal IEnumerable<Activity> GetActivitiesBeingTimed()
        {
            return from activity in _DataModel.ActivitiesTable
                   where activity.EndTime == DateTime.MaxValue
                   select new Activity(activity);
        }
    }
}
